// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var randomCode;
	var ws;
	var inventoryId;
	var ninventoryId;
	var databind = require("databind");
	
	var init = function() {
		randomCode = base.getUrlParam("randomCode");
		initEvent();
	};
	var initEvent = function() {
		
		setInterval(function() {
			getPassedData(randomCode, function(){
				if (ninventoryId != inventoryId) {
					inventoryId = ninventoryId;
					inquireBulkstockInventoryDetail(ninventoryId);
				} else {
					
				}
			});
		}, 1000);
	};
	
	
	//查询货箱信息
	var inquireBulkstockInventoryDetail = function(inventoryId){
		base.fnAjax({
            action: "inquireBulkstockInventoryDetail.json", //接口名称
            async: false, //同步异步
            oData: {
                inventoryId:inventoryId
            }, //传递参数
            fnSuccess: function(data) { //成功回调
                if (data.status == 200) {
//              	var tpl = $(".tpl").html();
//              	var html = juicer(tpl, data);
//              	$(".html").html(html);
					$(".uuuul").html("");
					data.datas[0].weight = data.datas[0].containerBulkstocks[0].weight;
					data.datas[0].bulkstockName = data.datas[0].containerBulkstocks[0].bulkstockName;
					data.datas[0].quantity = data.datas[0].containerBulkstocks[0].quantity;
					databind.fillForm($(".inventoryInfo"), data.datas[0]);
                	
					//理货前照片
					$.each(data.datas[0].tallyPreFile.split(','), function() {
						var _this = this;
						if (_this != null && $.trim(_this) != "") {
//							var str = '<li><div class="am-gallery-item magnify"><div class="magnify_glass"> </div><a href="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'" class=""><div class="element_to_magnify"><img class="loupe1" src="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'"  alt="理货前照片"/></div><h3 class="am-gallery-title">理货前照片</h3><div class="am-gallery-desc"></div></a></div></li>';
							var str = '<li><div class="am-gallery-item"><a href="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'" class=""><img class="loupe1" src="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'"  alt="理货前照片"/><h3 class="am-gallery-title">理货前照片</h3><div class="am-gallery-desc"></div></a></div></li>';
							$(".uuuul").append(str);
						}
					});
					
					//理货后照片
					$.each(data.datas[0].tallySuffixFile.split(','), function() {
						var _this = this;
						if (_this != null && $.trim(_this) != "") {
//							var str = '<li><div class="am-gallery-item magnify"><div class="magnify_glass"> </div><a href="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'" class=""><div class="element_to_magnify"><img class="loupe1" src="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'"  alt="理货后照片"/></div><h3 class="am-gallery-title">理货后照片</h3><div class="am-gallery-desc"></div></a></div></li>';
							var str = '<li><div class="am-gallery-item"><a href="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'" class=""><img class="loupe1" src="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'"  alt="理货后照片"/><h3 class="am-gallery-title">理货后照片</h3><div class="am-gallery-desc"></div></a></div></li>';
							$(".uuuul").append(str);
						}
					});
					
					//货物照片
					$.each(data.datas[0].containerBulkstocks, function() {
						$.each(this.filesUuid.split(','), function() {
							var _this = this;
							if (_this != null && $.trim(_this) != "") {
//								var str = '<li><div class="am-gallery-item magnify"><div class="magnify_glass"> </div><a href="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'" class=""><div class="element_to_magnify"><img class="loupe1" src="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'"  alt="货物照片"/></div><h3 class="am-gallery-title">货物照片</h3><div class="am-gallery-desc"></div></a></div></li>';
								var str = '<li><div class="am-gallery-item"><a href="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'" class=""><img class="loupe1" src="/finance/api/v1.0/downloadFile.file?fileUuid='+_this+'"  alt="货物照片"/><h3 class="am-gallery-title">货物照片</h3><div class="am-gallery-desc"></div></a></div></li>';
								$(".uuuul").append(str);
							}
						});
					});
					
					$('#loupe1').ClassyLoupe({
		                maxSize: 600,
		                size: 160,
		                loupeToggleSpeed: 'fast',
		                canZoom: true,
		                zoom:150
		            });
//					
//					$(".magnify").jfMagnify();
					$(".magnify").jfMagnify();
                } else {
                    base.alert(3, data.message);
                }
            }
        });
	};
	
	var getPassedData = function(randomCode, callback) {
		base.fnAjax({
			action: "getPassedData.json",
			oData: {
				randomCode: randomCode
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					ninventoryId = data.datas[0].data;
					callback();
				} else {
					base.alert(3, data.message);
				}
			}
		});
	}
	
	return {
		init: init
	};
});